#include "monitordevice.h"
#include "ui_monitordevice.h"
#include"modbusmanger.h"
#include"maininterface.h"
Q_GLOBAL_STATIC(MonitorDevice,theInstance);
MonitorDevice::MonitorDevice(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MonitorDevice)
{
    ui->setupUi(this);
    getSensorThresholdData(thresholdData);


}

MonitorDevice::~MonitorDevice()
{
    delete ui;
}

MonitorDevice *MonitorDevice::instance()
{
    return theInstance;
}

void MonitorDevice::MonitorSensorDataFunction(const sensorData &Data)
{

 extern  QVector<int>SensorWarningTimesVector;
    //对比传感器数值和阈值的比较(空气温度)
    if(Data.AirTemperature > thresholdData[0].alarmUpperLimit)//-1为过低 1为过高 0为正常
    {
        //emit//温度过高信号;
        emit sendAirTemperature(1);
        if(getAutoControlStatus())
            emit sendAutoControlAirTemperature(1);
        SensorDataStatus.airT=1;
        SensorWarningTimesVector[0]++;
        emit sendAirTemperatureToWarningInformation(0,1);
    }else if(Data.AirTemperature < thresholdData[0].alarmLowerLimit)
    {

        //emit//低温信号;
        emit sendAirTemperature(-1);
        if(getAutoControlStatus())
            emit sendAutoControlAirTemperature(-1);
        SensorDataStatus.airT=-1;
        SensorWarningTimesVector[0]++;
        emit sendAirTemperatureToWarningInformation(0,-1);
    }
    else{
     emit sendAirTemperature(0);
        if(getAutoControlStatus())
            emit sendAutoControlAirTemperature(0);
     SensorDataStatus.airT=0;
    }

    //对比传感器数值和阈值的比较(空气湿度)
    if(Data.AirHumidity > thresholdData[1].alarmUpperLimit)
    {
        //emit//湿度过大信号;
        emit sendAirHumidity(1);
        if(getAutoControlStatus())
            emit sendAutoControlAirHumidity(1);
        SensorDataStatus.airH=1;
        SensorWarningTimesVector[1]++;
        emit sendAirHumidityToWarningInformation(1,1);

    }else if(Data.AirHumidity < thresholdData[1].alarmLowerLimit)
    {
        //emit//湿度较低信号;
        emit sendAirHumidity(-1);
        if(getAutoControlStatus())
            emit sendAutoControlAirHumidity(-1);
        SensorDataStatus.airH=-1;
        SensorWarningTimesVector[1]++;
        emit sendAirHumidityToWarningInformation(1,-1);

    }else{

        emit sendAirHumidity(0);
        if(getAutoControlStatus())
            emit sendAutoControlAirHumidity(0);
        SensorDataStatus.airH=0;

    }
    //对比传感器数值和阈值的比较(光照强度)
    if(Data.LightIntensity > thresholdData[2].alarmUpperLimit)
    {
        //emit//光照强度过大信号;
        emit sendLightIntensity(1);
        if(getAutoControlStatus())
            emit sendAutoControlLightIntensity(1);
        SensorDataStatus.Light=1;
        SensorWarningTimesVector[2]++;
        emit sendLightIntensityToWarningInformation(2,1);

    }else if(Data.LightIntensity < thresholdData[2].alarmLowerLimit)
    {

        //emit//光照强度过低信号;
        emit sendLightIntensity(-1);
        if(getAutoControlStatus())
            emit sendAutoControlLightIntensity(-1);
        SensorDataStatus.Light=-1;
        SensorWarningTimesVector[2]++;
        emit sendLightIntensityToWarningInformation(2,-1);

    }else{
        emit sendLightIntensity(0);
        if(getAutoControlStatus())
            emit sendAutoControlLightIntensity(0);
        SensorDataStatus.Light=0;
    }

    //对比传感器数值和阈值的比较(CO2化碳浓度)
    if(Data.CO2Concentration > thresholdData[3].alarmUpperLimit)
    {
        //emit//CO2化碳浓度过高信号;
        emit sendCO2Concentration(1);
        if(getAutoControlStatus())
            emit sendAutoControlCO2Concentration(1);
        SensorDataStatus.CO2=1;
       SensorWarningTimesVector[3]++;
       emit sendCO2ConcentrationToWarningInformation(3,1);

    }else if(Data.CO2Concentration < thresholdData[3].alarmLowerLimit)
    {
         emit sendCO2Concentration(-1);
        if(getAutoControlStatus())
            emit sendAutoControlCO2Concentration(-1);
         SensorDataStatus.CO2=-1;
         SensorWarningTimesVector[3]++;
         emit sendCO2ConcentrationToWarningInformation(3,-1);
        //emit//CO2化碳浓度过低信号;
    }
    else{
        emit sendCO2Concentration(0);
        if(getAutoControlStatus())
            emit sendAutoControlCO2Concentration(0);
         SensorDataStatus.CO2=0;
    }
    //对比传感器数值和阈值的比较(土壤湿度)
    if(Data.SoilMoisture > thresholdData[4].alarmUpperLimit)
    {
        //emit//土壤湿度过大信号;
        emit sendSoilMoisture(1);
        if(getAutoControlStatus())
            emit sendAutoControlSoilMoisture(1);
         SensorDataStatus.soilH=1;
         SensorWarningTimesVector[4]++;
         emit sendSoilMoistureToWarningInformation(4,1);

    }else if(Data.SoilMoisture < thresholdData[4].alarmLowerLimit)
    {

        //emit//土壤湿度过低信号;
        emit sendSoilMoisture(-1);
        if(getAutoControlStatus())
            emit sendAutoControlSoilMoisture(-1);
        SensorDataStatus.soilH=-1;
        SensorWarningTimesVector[4]++;
        emit sendSoilMoistureToWarningInformation(4,-1);
    }
    else{

        emit sendSoilMoisture(0);
        if(getAutoControlStatus())
            emit sendAutoControlSoilMoisture(0);
        SensorDataStatus.soilH=0;

    }

if(SensorDataStatus.airT!=0||SensorDataStatus.airH!=0||SensorDataStatus.Light!=0||SensorDataStatus.CO2!=0||SensorDataStatus.soilH!=0)
{

emit sendWarningLed(false);

}
else{
emit sendWarningLed(true);

}





}

void MonitorDevice::receiveAutoControlSignal(int status)
{
   autoControlStatus=status;
}

bool MonitorDevice::getSensorThresholdData(QVector<SensorThresholdData> &thresholdData)
{
    if(UserMysqlS::instance()->getSensorThresholdData(thresholdData))
    {
        for(QVector<SensorThresholdData>::Iterator it=thresholdData.begin();it!=thresholdData.end();it++)
        {

          qDebug()<<it->id<<" "<<it->sensorName<<" "<<it->alarmUpperLimit<<" "<<it->alarmLowerLimit<<" "<<it->alarmUpperLimitHappend<<" "<<it->alarmLowerLimitHappend<<endl;
        }

    }
}

void MonitorDevice::initSensorDataStatus(sensorDataStatus& Data)
{
Data.airT=0;
Data.airH=0;
Data.Light=0;
Data.CO2=0;
Data.soilH=0;
}

bool MonitorDevice::getAutoControlStatus()
{
    extern bool AutoControlStatus2;
    return AutoControlStatus2;
}
